﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;
using TradeForm.Properties;
using System.Configuration;
using TradeForm.config;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace TradeForm.Print
{
    public class ProformaInvoiceDocument : DocumentBase
    {
        //放置配置的字典
        protected Dictionary<string, Setting> dicPrintSets = new Dictionary<string, Setting>();

        public ProformaInvoiceDocument()
        { }
        public ProformaInvoiceDocument(string defcode)
        {
            this._defCode = defcode;
            getData(defcode);
            loadPrintSettings();

        }

        public ProformaInvoiceDocument(string defcode, bool isPrintBack)
        {
            this.IsPrintBack = isPrintBack;
            this._defCode = defcode;
            getData(defcode);
            loadPrintSettings();
        }

        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            g.PageUnit = GraphicsUnit.Millimeter;
            float fontHeight = g.MeasureString("字体测试高度", base.Font).Height;
            if (this.IsPrintBack)
            {
                if (File.Exists(Application.StartupPath + @"\BackImage\形式发票.JPG"))
                {
                    g.DrawImage(Image.FromFile(Application.StartupPath + @"\BackImage\形式发票.JPG"), 0, 0, Convert.ToSingle(e.PageBounds.Width * 25.4 / 100),
                   Convert.ToSingle(e.PageBounds.Height * 25.4 / 100));
                }

            }
            if (this.pinv != null && this.pinvGoodsList != null && this.pinvGoodsList.Count > 0)
            {


                g.DrawString(pinv.Consignee, base.Font, Brushes.Black, dicPrintSets["Reciever"].PosX, dicPrintSets["Reciever"].PosY);
                //g.DrawString(pinv.DestPort, base.Font, Brushes.Black, dicPrintSets["ToPort"].PosX, dicPrintSets["ToPort"].PosY);
                // g.DrawString(pinv.FromPort, base.Font, Brushes.Black, dicPrintSets["FromPort"].PosX, dicPrintSets["FromPort"].PosY);

                g.DrawString(pinv.InvoiceNo, base.Font, Brushes.Black, dicPrintSets["InvoiceNo"].PosX, dicPrintSets["InvoiceNo"].PosY);
                //g.DrawString(pinv.ContractNo, base.Font, Brushes.Black, dicPrintSets["ContractNo"].PosX, dicPrintSets["ContractNo"].PosY);
                g.DrawString(pinv.InvoiceDate.Value.ToString("yyyy-MM-dd"), base.Font, Brushes.Black, dicPrintSets["InvoiceDate"].PosX, dicPrintSets["InvoiceDate"].PosY);

                g.DrawString(pinv.PriceTerms == "FOB" ? (pinv.PriceTerms + " " + pinv.FromPort) : (pinv.PriceTerms + " " + pinv.DestPort),
                    base.Font, Brushes.Black, dicPrintSets["PriceTerms"].PosX, dicPrintSets["PriceTerms"].PosY);

                int i = 0;
                float goodspan = 10.0f;
                decimal totalAmount = 0.0m;
                string currency = string.Empty;
                foreach (ProFormaInvoiceGoods item in this.pinvGoodsList)
                {
                    totalAmount += item.Amount.Value;
                    g.DrawString(item.GoodsName, base.Font, Brushes.Black, dicPrintSets["GoodsCName"].PosX,
                        dicPrintSets["GoodsCName"].PosY + i * goodspan);
                    g.DrawString(item.Qty.Value.ToString("0") + item.QtyUnit, base.Font, Brushes.Black, dicPrintSets["Qty"].PosX,
                        dicPrintSets["Qty"].PosY + i * goodspan);
                    //g.DrawString(item.Currency, base.Font, Brushes.Black, dicPrintSets["UnitPrice"].PosX - 8,
                    //    dicPrintSets["UnitPrice"].PosY + i * goodspan);
                    g.DrawString(item.UnitPrice.Value.ToString("###,##0.00"), base.Font, Brushes.Black, dicPrintSets["UnitPrice"].PosX,
                        dicPrintSets["UnitPrice"].PosY + i * goodspan);
                    g.DrawString(item.Amount.Value.ToString("###,##0.00"), base.Font, Brushes.Black, dicPrintSets["Amount"].PosX,
                        dicPrintSets["Amount"].PosY + i * goodspan);
                    currency = item.Currency;
                    i++;
                }
                g.DrawString(pinv.Mark, base.Font, Brushes.Black, dicPrintSets["Remark"].PosX, dicPrintSets["Remark"].PosY);
                g.DrawString(pinv.GatherInfo, base.Font, Brushes.Black, dicPrintSets["GatherInfo"].PosX, dicPrintSets["GatherInfo"].PosY);
                g.DrawString("***********************************************************************************", base.Font, Brushes.Black, 30.0f, dicPrintSets["GoodsCName"].PosY + goodspan * i + 2);
                //g.DrawString(_bill.PayeeBank, base.Font, Brushes.Black, dicPrintSets["TTTo"].PosX, dicPrintSets["TTTo"].PosY);
                //g.DrawString(_bill.PayeeACNo, base.Font, Brushes.Black, dicPrintSets["ACNo"].PosX, dicPrintSets["ACNo"].PosY);
                //g.DrawString(_bill.PayeeSwiftCode, base.Font, Brushes.Black, dicPrintSets["SWIFT"].PosX, dicPrintSets["SWIFT"].PosY);
                //g.DrawString(_bill.PayeeBank, base.Font, Brushes.Black, dicPrintSets["PAYEE"].PosX, dicPrintSets["PAYEE"].PosY);
                //g.DrawString(totalAmount.ToString() + currency, base.Font, Brushes.Black, dicPrintSets["Total"].PosX, dicPrintSets["Total"].PosY);

                //g.DrawString(_bill.Freight, base.Font, Brushes.Black, dicPrintSets["Freight"].PosX, dicPrintSets["Freight"].PosY);
                //g.DrawString(_bill.ShipmentDate.Value.ToString("yyyy-MM-dd"), base.Font, Brushes.Black, dicPrintSets["ShipmentDate"].PosX, dicPrintSets["ShipmentDate"].PosY);
                //g.DrawString(_bill.Insurance, base.Font, Brushes.Black, dicPrintSets["Insurance"].PosX, dicPrintSets["Insurance"].PosY);
                //g.DrawString(_bill.Remittanceway, base.Font, Brushes.Black, dicPrintSets["Payment"].PosX, dicPrintSets["Payment"].PosY);
            }
            if (this._compny != null)
            {
                float widthCName = g.MeasureString(_compny.CName, base.TitleFont).Width;
                float widthEName = g.MeasureString(_compny.EName, base.TitleFont).Width;
                float widthEAddress = g.MeasureString(_compny.EAddress, base.Font).Width;
                float widthTel = g.MeasureString(_compny.Tel, base.Font).Width;

                g.DrawString(_compny.CName, base.TitleFont, Brushes.Black, Convert.ToSingle((210.0 - widthCName) / 2), dicPrintSets["CompanyCName"].PosY);
                g.DrawString(_compny.EName, base.TitleFont, Brushes.Black, Convert.ToSingle((210.0 - widthEName) / 2), dicPrintSets["CompanyEName"].PosY);
                g.DrawString(_compny.EAddress, base.Font, Brushes.Black, Convert.ToSingle((210.0 - widthEAddress) / 2), dicPrintSets["EAddress"].PosY);
                g.DrawString(_compny.Tel, base.Font, Brushes.Black, Convert.ToSingle((210.0 - widthTel) / 2), dicPrintSets["Tel"].PosY);

            }
        }

        protected void loadPrintSettings()
        {
            XElement printSettings = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + @"\config\ProformaInvoiceSettings.xml");
            IEnumerable<XElement> settings = null;
            settings = (from el in printSettings.Elements("setting")
                        select el);
            if (settings != null && settings.Count() > 0)
            {
                foreach (XElement item in settings)
                {
                    if (!dicPrintSets.ContainsKey(item.Attribute("name").Value))
                    {

                        Setting set = new Setting();
                        set.Name = item.Attribute("name").Value;
                        set.CName = item.Attribute("cname").Value;
                        set.PosX = Convert.ToSingle(item.Attribute("PosX").Value);
                        set.PosY = Convert.ToSingle(item.Attribute("PosY").Value);
                        dicPrintSets.Add(set.Name, set);
                    }
                    else
                    {
                        dicPrintSets[item.Attribute("name").Value].CName = item.Attribute("cname").Value;
                        dicPrintSets[item.Attribute("name").Value].PosX = Convert.ToSingle(item.Attribute("PosX").Value);
                        dicPrintSets[item.Attribute("name").Value].PosY = Convert.ToSingle(item.Attribute("PosY").Value);
                    }
                }

            }
            else
            {

            }

        }
    }
}
